<?php
class UserController extends SCController {
	
	public function __construct() {
		parent::__construct();
	}
	
	public function index() {
		$username = SCRequest::getControllerName();
		$userModel = new UserModel();
		$user = $this->model->field('id, username')->from('user')->where('username = "'.$username.'"')->limit(1)->find();
		$user || $this->redirect->goto('index');
		
		if(false != ($user_id = $userModel->checkLogin())) { // 已登录
			if($user->id == $user_id) { // 自己主页
				die($this->view->render('user/home'));
			}
		}
		
		$this->view->render('user/index', array('user' => json_encode($user)));
	}
	
	/**
	 * 
	 * 处理用户登录
	 */
	public function login() {
		SCRequest::checkPost() || $this->ajaxFail('illegal_request');
		
		$userModel = new UserModel();
		$email = strtolower(SCRequest::post('email'));
		$password = SCRequest::post('password');
		
		if(!($user_id = $userModel->checkAcount($email, $password))) {
			$this->ajaxFail();
		} else {
			$userModel->setLogin($user_id);
			$this->ajaxSuccess($userModel->getBasicInfo($user_id));
		}
	}
	
	protected function checkLogin() {
		if(false != ($user_id = SCScabish::model('user')->checkLogin())) {
			return $user_id;
		} else {
			$userController = new UserController();
			SCRequest::setActionName('login');
			$userController->fetchLoginUI();
			exit;
		}
	}
	
	/**
	 * 
	 * 用户退出
	 */
	public function logout() {
		SCScabish::model('user')->setLogout();
		$this->redirect->to();
	}
	
	/**
	 * 
	 * 判断是否登录
	 */
	public function isLogin() {
		$userModel = new UserModel();
		$user_id = $userModel->checkLogin();
		if($user_id) {
			$this->ajaxSuccess($userModel->getBasicInfo($user_id));
		} else {
			$this->ajaxFail();
		}
	}
	
	/**
	 * 
	 * 获取当前登录用户的基本信息
	 */
	public function getBasicInfo() {
		$userModel = new UserModel();
		$user_id = $userModel->checkLogin();
		if($user_id) {
			$this->ajaxSuccess($userModel->getBasicInfo($user_id));
		} else {
			$this->ajaxFail();
		}
	}
	
	/**
	 * 
	 * 发送Thing
	 */
	public function sendThing() {
		SCRequest::checkPost() || $this->ajaxFail();
		($user_id = $this->checkLogin()) || $this->ajaxFail();
		
		$data = array(
			'user_id' => $user_id,
			'type' => ThingModel::$_thingType['thing'],
			'content' => $this->reviseContent(SCRequest::post('content')),
			'post_date' => date('Y-m-d H:i:s')
		);
		
		$thingId = $this->model->from('thing')->add($data);
		
		$thingModel = new ThingModel();
		$this->ajaxSuccess($thingModel->getThing($thingId));
	}
	
	/**
	 * 
	 * 修正内容
	 * @param string $content
	 */
	protected function reviseContent($content) {
		$content = preg_replace('/(<br \/>){2, }/', '\1', nl2br($content));
		$content = preg_replace('/(<br\s*\/>)*/', '\1', $content);
		$content = preg_replace('/^<p>(.*)<\/p>$/', '\1', $content);
		return preg_replace('/(.*)<br(\s)*\/>$/', '\1', $content);
	}
	
	/**
	 * 
	 * 删除Thing
	 */
	public function deleteThing() {
		$id = !is_null(SCRequest::get('id')) ? SCRequest::get('id') : $this->ajaxFail();
		$userModel = new UserModel();
		($user_id = $userModel->checkLogin()) || $this->ajaxFail();
		
		$this->model->from('thing')->where('id = '.$id.' AND user_id = '.$user_id)->delete();
		
		$this->ajaxSuccess();
	}
	
	/**
	 * 
	 * 获取更早之前发布的thing
	 * $id 分界点ID 
	 */
	public function getThings() {
		$id = !is_null(SCRequest::get('id')) ? SCRequest::get('id') : $this->ajaxFail();
		$ThingModel = new ThingModel();
		
		$this->ajaxSuccess($ThingModel->getThings($id));	
	}
	
	/**
	 * 
	 * 获取博客撰写UI
	 */
	public function fetchWriteBlogUI() {
		$this->view->render();
	}
	
	/**
	 * 
	 * 发送博客
	 */
	public function sendBlog() {
		SCRequest::checkPost() || $this->ajaxFail();
		
		// 保存博客
		$tblog_id = $this->model->from('tblog')->add(array( 
			'user_id' => 1,
			'title' => SCRequest::post('title'),
			'content' => SCRequest::post('content'),
			'post_date' => date('Y-m-d H:i:s')
		));
		
		// 推送到thing动态上
		$this->model->from('thing')->add(array(
			'user_id' => 1,
			'type' => ThingModel::translateType('blog'),
			'related_id' => $tblog_id,
			'post_date' => date('Y-m-d H:i:s')
		));
		
		$this->ajaxSuccess();
	}
	
	/**
	 * 
	 * 获取thing的回应
	 */
	public function getThingComments() {
		$id = !is_null(SCRequest::get('id')) ? SCRequest::get('id') : $this->ajaxFail();
		$commentModel = new CommentModel();
		$this->ajaxSuccess($commentModel->getComments($id, 'thing'));
	}
	
	/**
	 * 
	 * 删除回应
	 */
	public function deleteComment() {
		$id = !is_null(SCRequest::get('id')) ? SCRequest::get('id') : $this->ajaxFail();
		$userModel = new UserModel();
		($user_id = $userModel->checkLogin()) || $this->ajaxFail();
		$this->model->from('comment')->where('id = '.$id.' AND user_id = '.$user_id)->delete();
		$this->ajaxSuccess();
	}
	
	/**
	 * 
	 * 获取指定thing留言数
	 */
	public function getCommentCount() {
		$id = !is_null(SCRequest::get('id')) ? SCRequest::get('id') : $this->ajaxFail();
		$commentModel = new CommentModel();
		$this->ajaxSuccess(array('count' => $commentModel->getCommentCount($id, 'thing')));
	}
	
	/**
	 * 
	 * 获取照片上传UI
	 */
	public function fetchUploadPhotoUI() {
		$this->checkLogin();
		$this->view->render();
	}
	
	/**
	 * 
	 * 处理从本地上传的图片
	 */
	public function doUploadPhoto() {
		$PhotoModel = new PhotoModel();
		exit($PhotoModel->uploadPhoto());
	}
	
	/**
	 * 
	 * 处理外链图片
	 */
	public function doLinkPhoto() {
		$PhotoModel = new PhotoModel();
		exit($PhotoModel->linkPhoto());
	}
	
	/**
	 * 
	 * 完成照片上传
	 */
	public function finishUploadPhoto() {
		$data = !is_null(SCRequest::post('photo')) ? SCRequest::post() : $this->ajaxFail();
		
		// 保存图片记录事件
		$tphoto_id = $this->model->add(array(
			'user_id' => 1,
			'content' => $data['content'],
			'longitude' => $data['longitude'],
			'latitude' => $data['latitude'],
			'post_date' => date('Y-m-d H:i:s')
		), 'tphoto');
		
		// 保存图片信息
		foreach($data['photo'] as $photo) {
			$this->model->add(array(
				'user_id' => 1,
				'tphoto_id' => $tphoto_id,
				'thumb' => $photo['thumb'],
				'url' => $photo['url']
			), 'photo');
		}
		
		// 推送到thing动态上
		$this->model->from('thing')->add(array(
			'user_id' => 1,
			'type' => ThingModel::translateType('photo'),
			'related_id' => $tphoto_id,
			'post_date' => date('Y-m-d H:i:s')
		));
		
		exit('1');
	}
	
	/**
	 * 
	 * 喜欢某个thing
	 */
	public function likeThing() {
		$id = !is_null(SCRequest::get('id')) ? SCRequest::get('id') : $this->ajaxFail();
		
		$userModel = new UserModel();
		$likerModel = new LikerModel();
		if($likerModel->setThingLike($id, $userModel->checkLogin())) {
			$this->ajaxSuccess();
		} else {
			$this->ajaxFail();
		}
	}
}